home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / resources.pak / Unnamed File 000067.txt < prev    next >
Text File  |  2013-04-03  |  7KB  |  250 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. cr.define('cr.ui', function() {
  6.   /** @const */ var Event = cr.Event;
  7.   /** @const */ var EventTarget = cr.EventTarget;
  8.  
  9.   /**
  10.    * Creates a new selection model that is to be used with lists. This only
  11.    * allows a single index to be selected.
  12.    *
  13.    * @param {number=} opt_length The number items in the selection.
  14.    *
  15.    * @constructor
  16.    * @extends {!cr.EventTarget}
  17.    */
  18.   function ListSingleSelectionModel(opt_length) {
  19.     this.length_ = opt_length || 0;
  20.     this.selectedIndex = -1;
  21.  
  22.     // True if any item could be lead or anchor. False if only selected ones.
  23.     this.independentLeadItem_ = !cr.isMac && !cr.isChromeOS;
  24.   }
  25.  
  26.   ListSingleSelectionModel.prototype = {
  27.     __proto__: EventTarget.prototype,
  28.  
  29.     /**
  30.      * The number of items in the model.
  31.      * @type {number}
  32.      */
  33.     get length() {
  34.       return this.length_;
  35.     },
  36.  
  37.     /**
  38.      * @type {!Array} The selected indexes.
  39.      */
  40.     get selectedIndexes() {
  41.       var i = this.selectedIndex;
  42.       return i != -1 ? [this.selectedIndex] : [];
  43.     },
  44.     set selectedIndexes(indexes) {
  45.       this.selectedIndex = indexes.length ? indexes[0] : -1;
  46.     },
  47.  
  48.     /**
  49.      * Convenience getter which returns the first selected index.
  50.      * Setter also changes lead and anchor indexes if value is nonegative.
  51.      * @type {number}
  52.      */
  53.     get selectedIndex() {
  54.       return this.selectedIndex_;
  55.     },
  56.     set selectedIndex(selectedIndex) {
  57.       var oldSelectedIndex = this.selectedIndex;
  58.       var i = Math.max(-1, Math.min(this.length_ - 1, selectedIndex));
  59.  
  60.       if (i != oldSelectedIndex) {
  61.         this.beginChange();
  62.         this.selectedIndex_ = i;
  63.         this.leadIndex = i >= 0 ? i : this.leadIndex;
  64.         this.endChange();
  65.       }
  66.     },
  67.  
  68.     /**
  69.      * Selects a range of indexes, starting with {@code start} and ends with
  70.      * {@code end}.
  71.      * @param {number} start The first index to select.
  72.      * @param {number} end The last index to select.
  73.      */
  74.     selectRange: function(start, end) {
  75.       // Only select first index.
  76.       this.selectedIndex = Math.min(start, end);
  77.     },
  78.  
  79.     /**
  80.      * Selects all indexes.
  81.      */
  82.     selectAll: function() {
  83.       // Select all is not allowed on a single selection model
  84.     },
  85.  
  86.     /**
  87.      * Clears the selection
  88.      */
  89.     clear: function() {
  90.       this.beginChange();
  91.       this.length_ = 0;
  92.       this.selectedIndex = this.anchorIndex = this.leadIndex = -1;
  93.       this.endChange();
  94.     },
  95.  
  96.     /**
  97.      * Unselects all selected items.
  98.      */
  99.     unselectAll: function() {
  100.       this.selectedIndex = -1;
  101.     },
  102.  
  103.     /**
  104.      * Sets the selected state for an index.
  105.      * @param {number} index The index to set the selected state for.
  106.      * @param {boolean} b Whether to select the index or not.
  107.      */
  108.     setIndexSelected: function(index, b) {
  109.       // Only allow selection
  110.       var oldSelected = index == this.selectedIndex_;
  111.       if (oldSelected == b)
  112.         return;
  113.  
  114.       if (b)
  115.         this.selectedIndex = index;
  116.       else if (index == this.selectedIndex_)
  117.         this.selectedIndex = -1;
  118.     },
  119.  
  120.     /**
  121.      * Whether a given index is selected or not.
  122.      * @param {number} index The index to check.
  123.      * @return {boolean} Whether an index is selected.
  124.      */
  125.     getIndexSelected: function(index) {
  126.       return index == this.selectedIndex_;
  127.     },
  128.  
  129.     /**
  130.      * This is used to begin batching changes. Call {@code endChange} when you
  131.      * are done making changes.
  132.      */
  133.     beginChange: function() {
  134.       if (!this.changeCount_) {
  135.         this.changeCount_ = 0;
  136.         this.selectedIndexBefore_ = this.selectedIndex_;
  137.       }
  138.       this.changeCount_++;
  139.     },
  140.  
  141.     /**
  142.      * Call this after changes are done and it will dispatch a change event if
  143.      * any changes were actually done.
  144.      */
  145.     endChange: function() {
  146.       this.changeCount_--;
  147.       if (!this.changeCount_) {
  148.         if (this.selectedIndexBefore_ != this.selectedIndex_) {
  149.           var beforeChange = this.createChangeEvent('beforeChange');
  150.           if (this.dispatchEvent(beforeChange))
  151.             this.dispatchEvent(this.createChangeEvent('change'));
  152.           else
  153.             this.selectedIndex_ = this.selectedIndexBefore_;
  154.         }
  155.       }
  156.     },
  157.  
  158.     /**
  159.      * Creates event with specified name and fills its {changes} property.
  160.      * @param {String} name Event name.
  161.      */
  162.     createChangeEvent: function(eventName) {
  163.       var e = new Event(eventName);
  164.       var indexes = [this.selectedIndexBefore_, this.selectedIndex_];
  165.       e.changes = indexes.filter(function(index) {
  166.         return index != -1;
  167.       }).map(function(index) {
  168.         return {
  169.           index: index,
  170.           selected: index == this.selectedIndex_
  171.         };
  172.       }, this);
  173.  
  174.       return e;
  175.     },
  176.  
  177.     leadIndex_: -1,
  178.  
  179.     /**
  180.      * The leadIndex is used with multiple selection and it is the index that
  181.      * the user is moving using the arrow keys.
  182.      * @type {number}
  183.      */
  184.     get leadIndex() {
  185.       return this.leadIndex_;
  186.     },
  187.     set leadIndex(leadIndex) {
  188.       var li = this.adjustIndex_(leadIndex);
  189.       if (li != this.leadIndex_) {
  190.         var oldLeadIndex = this.leadIndex_;
  191.         this.leadIndex_ = li;
  192.         cr.dispatchPropertyChange(this, 'leadIndex', li, oldLeadIndex);
  193.         cr.dispatchPropertyChange(this, 'anchorIndex', li, oldLeadIndex);
  194.       }
  195.     },
  196.  
  197.     adjustIndex_: function(index) {
  198.       index = Math.max(-1, Math.min(this.length_ - 1, index));
  199.       if (!this.independentLeadItem_)
  200.         index = this.selectedIndex;
  201.       return index;
  202.     },
  203.  
  204.     /**
  205.      * The anchorIndex is used with multiple selection.
  206.      * @type {number}
  207.      */
  208.     get anchorIndex() {
  209.       return this.leadIndex;
  210.     },
  211.     set anchorIndex(anchorIndex) {
  212.       this.leadIndex = anchorIndex;
  213.     },
  214.  
  215.     /**
  216.      * Whether the selection model supports multiple selected items.
  217.      * @type {boolean}
  218.      */
  219.     get multiple() {
  220.       return false;
  221.     },
  222.  
  223.     /**
  224.      * Adjusts the selection after reordering of items in the table.
  225.      * @param {!Array.<number>} permutation The reordering permutation.
  226.      */
  227.     adjustToReordering: function(permutation) {
  228.       if (this.leadIndex != -1)
  229.         this.leadIndex = permutation[this.leadIndex];
  230.  
  231.       var oldSelectedIndex = this.selectedIndex;
  232.       if (oldSelectedIndex != -1) {
  233.         this.selectedIndex = permutation[oldSelectedIndex];
  234.       }
  235.     },
  236.  
  237.     /**
  238.      * Adjusts selection model length.
  239.      * @param {number} length New selection model length.
  240.      */
  241.     adjustLength: function(length) {
  242.       this.length_ = length;
  243.     }
  244.   };
  245.  
  246.   return {
  247.     ListSingleSelectionModel: ListSingleSelectionModel
  248.   };
  249. });
  250.